#include "Shiny.h"
#include "Ray.h"
#include "Scene.h"

Shiny::Shiny(const Vector3 & ks) :
    m_ks(ks)
{

}

Shiny::~Shiny()
{
}

Vector3 Shiny::shade(const Ray& ray, const HitInfo& hit, char rayCount, const Scene& scene, int mode, Vector3 rayColor, bool diffuseRay) const
{
    if(rayCount < MAX_REFLECTIONS)
	{
		Vector3 reflection = ray.d - 2*(dot(ray.d,hit.N))*hit.N;
		reflection.normalize();
	
		Ray newRay(hit.P, reflection);
		HitInfo newHitInfo;
		if (scene.trace(newHitInfo, newRay, epsilon))
		{
			Vector3 L = m_ks * newHitInfo.material->shade(newRay, newHitInfo, rayCount+1, scene, mode, m_ks * rayColor);
			return L;
		}
	}

	return Vector3(0.0f, 0.0f, 0.0f);
}

void Shiny::handlePhoton(const Ray& ray, const HitInfo& hit, char rayCount, Vector3 photonFlux, const Scene& scene, int mode, LightPath *lp) const
{
    if(rayCount < MAX_REFLECTIONS)
	{
		Vector3 reflection = ray.d - 2*(dot(ray.d,hit.N))*hit.N;
		reflection.normalize();

		Ray newRay(hit.P, reflection);
		HitInfo newHitInfo;
		if (scene.trace(newHitInfo, newRay, epsilon))
		{
			newHitInfo.material->handlePhoton(newRay, newHitInfo, rayCount+1, m_ks * photonFlux, scene, mode, lp);
		}
	}
}
